package com.sqlcheck.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.List;
import java.util.Map;

@Data
@Schema(description = "数据库DDL提取响应")
public class DatabaseDdlExtractionResponse {

    @Schema(description = "提取是否成功")
    private boolean success;

    @Schema(description = "消息")
    private String message;

    @Schema(description = "提取的DDL数据")
    private DdlData ddlData;

    @Schema(description = "提取统计信息")
    private ExtractionStats stats;

    @Data
    @Schema(description = "DDL数据")
    public static class DdlData {
        @Schema(description = "表DDL语句")
        private List<TableDdl> tables;

        @Schema(description = "视图DDL语句")
        private List<ViewDdl> views;

        @Schema(description = "索引DDL语句")
        private List<IndexDdl> indexes;

        @Schema(description = "约束DDL语句")
        private List<ConstraintDdl> constraints;

        @Schema(description = "存储过程DDL语句")
        private List<ProcedureDdl> procedures;

        @Schema(description = "函数DDL语句")
        private List<FunctionDdl> functions;

        @Schema(description = "触发器DDL语句")
        private List<TriggerDdl> triggers;
    }

    @Data
    @Schema(description = "表DDL")
    public static class TableDdl {
        @Schema(description = "表名")
        private String tableName;

        @Schema(description = "Schema名称")
        private String schemaName;

        @Schema(description = "创建表的DDL语句")
        private String createStatement;

        @Schema(description = "表注释")
        private String comment;

        @Schema(description = "表的列信息")
        private List<ColumnInfo> columns;
    }

    @Data
    @Schema(description = "列信息")
    public static class ColumnInfo {
        @Schema(description = "列名")
        private String columnName;

        @Schema(description = "数据类型")
        private String dataType;

        @Schema(description = "是否可为空")
        private boolean nullable;

        @Schema(description = "默认值")
        private String defaultValue;

        @Schema(description = "列注释")
        private String comment;

        @Schema(description = "是否为主键")
        private boolean primaryKey;

        @Schema(description = "是否自增")
        private boolean autoIncrement;
    }

    @Data
    @Schema(description = "视图DDL")
    public static class ViewDdl {
        @Schema(description = "视图名")
        private String viewName;

        @Schema(description = "Schema名称")
        private String schemaName;

        @Schema(description = "创建视图的DDL语句")
        private String createStatement;

        @Schema(description = "视图注释")
        private String comment;
    }

    @Data
    @Schema(description = "索引DDL")
    public static class IndexDdl {
        @Schema(description = "索引名")
        private String indexName;

        @Schema(description = "表名")
        private String tableName;

        @Schema(description = "Schema名称")
        private String schemaName;

        @Schema(description = "创建索引的DDL语句")
        private String createStatement;

        @Schema(description = "是否唯一索引")
        private boolean unique;

        @Schema(description = "索引列")
        private List<String> columns;
    }

    @Data
    @Schema(description = "约束DDL")
    public static class ConstraintDdl {
        @Schema(description = "约束名")
        private String constraintName;

        @Schema(description = "表名")
        private String tableName;

        @Schema(description = "Schema名称")
        private String schemaName;

        @Schema(description = "约束类型")
        private String constraintType;

        @Schema(description = "创建约束的DDL语句")
        private String createStatement;
    }

    @Data
    @Schema(description = "存储过程DDL")
    public static class ProcedureDdl {
        @Schema(description = "存储过程名")
        private String procedureName;

        @Schema(description = "Schema名称")
        private String schemaName;

        @Schema(description = "创建存储过程的DDL语句")
        private String createStatement;
    }

    @Data
    @Schema(description = "函数DDL")
    public static class FunctionDdl {
        @Schema(description = "函数名")
        private String functionName;

        @Schema(description = "Schema名称")
        private String schemaName;

        @Schema(description = "创建函数的DDL语句")
        private String createStatement;
    }

    @Data
    @Schema(description = "触发器DDL")
    public static class TriggerDdl {
        @Schema(description = "触发器名")
        private String triggerName;

        @Schema(description = "表名")
        private String tableName;

        @Schema(description = "Schema名称")
        private String schemaName;

        @Schema(description = "创建触发器的DDL语句")
        private String createStatement;
    }

    @Data
    @Schema(description = "提取统计信息")
    public static class ExtractionStats {
        @Schema(description = "提取的表数量")
        private int tableCount;

        @Schema(description = "提取的视图数量")
        private int viewCount;

        @Schema(description = "提取的索引数量")
        private int indexCount;

        @Schema(description = "提取的约束数量")
        private int constraintCount;

        @Schema(description = "提取的存储过程数量")
        private int procedureCount;

        @Schema(description = "提取的函数数量")
        private int functionCount;

        @Schema(description = "提取的触发器数量")
        private int triggerCount;

        @Schema(description = "提取耗时（毫秒）")
        private long extractionTimeMs;

        @Schema(description = "数据库信息")
        private Map<String, Object> databaseInfo;
    }

    public static DatabaseDdlExtractionResponse success(DdlData ddlData, ExtractionStats stats) {
        DatabaseDdlExtractionResponse response = new DatabaseDdlExtractionResponse();
        response.setSuccess(true);
        response.setMessage("DDL提取成功");
        response.setDdlData(ddlData);
        response.setStats(stats);
        return response;
    }

    public static DatabaseDdlExtractionResponse failure(String message) {
        DatabaseDdlExtractionResponse response = new DatabaseDdlExtractionResponse();
        response.setSuccess(false);
        response.setMessage(message);
        return response;
    }
}